package com.yunbiao.ybboardapi.utils;

import android.content.Context;
import android.media.AudioManager;

public class SoundContral {
    private static final String TAG = "SoundContral";

    private static SoundContral soundContral;

    private SoundContral(){}

    public static SoundContral getInstance(){
        if (soundContral==null){
            soundContral=new SoundContral();
        }
        return soundContral;
    }

    public AudioManager getAudioManager(Context context){
        if (context==null) return null;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        return audioManager;
    }

    public void setVolume(Context context,int value){
        setVolume(context,AudioManager.STREAM_MUSIC,value);
    }

    public int getCurrentVolume(Context context){
        return getVolume(context,AudioManager.STREAM_MUSIC);
    }

    public int getMinVolume(Context context){
        if (context==null) return -1;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        int minVolume = 0;
        try {
            minVolume = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minVolume;
    }

    public int getMaxVolume(Context context){
        if (context==null) return -1;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    }

    public int getVolume(Context context,int streamType){
        if (context==null) return -1;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        return audioManager.getStreamVolume(streamType);
    }

    public void setVolume(Context context,int streamType,int value){
        if (context==null) return;
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//        int minVolume = audioManager.getStreamMinVolume(streamType);
//        int maxVolume = audioManager.getStreamMaxVolume(streamType);
//        Log.d(TAG, "setVoice: minVolume->"+minVolume+", maxVolume->"+maxVolume);
//        if (value<minVolume){
//            value=minVolume;
//        }
//        if (value>maxVolume){
//            value=maxVolume;
//        }
        audioManager.setStreamVolume(streamType,value,AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_PLAY_SOUND);
    }
}
